//
//  BaseTabPanel.m
//  AsyncImages
//
//  Created by Matthew on 12-1-1.
//  Copyright 2012年 develop4u.gr. All rights reserved.
//

#import "BaseTabPanel.h"
#import "MWFeedItem.h"
#import "ColorUtil.h"

#define PAGE_WIDTH 320
#define PAGE_HEIGHT 480

#define LEVEL_1_TABLE_HEIGHT 310
#define LEVEL_2_TABLE_HEIGHT 365

#define TABLE_OFFSET_Y 140

#define TABLE_ROW_HEIGHT 66

@implementation BaseTabPanel

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) 
    {
        // Initialization code
    }
    return self;
}

-(void) initWithTags:(NSArray *) inTagArr
             topBarTitle:(NSString *) inTopBarTitle
                      suffix:(NSString *) inSuffix
                table1url:(NSString *) inTable1URL
                table2url:(NSString *) inTable2URL
                table3url:(NSString *) inTable3URL
        table1_2_link:(NSString *) inTable1_2URL
        table2_2_link:(NSString *) inTable2_2URL
    detal_table_link:(NSString *) inTableDetailURL
{
    detail_panel_shift_in = [[NSString alloc] initWithFormat:@"detail_panel_shift_in_%@",inSuffix];
    
    unit_trigger = [[NSString alloc] initWithFormat:@"unit_trigger_%@",inSuffix];
    unit_trigger_1_2 = [[NSString alloc] initWithFormat:@"unit_trigger_%@",@"table1_2"];
    unit_trigger_3_1 = [[NSString alloc] initWithFormat:@"unit_trigger_%@",@"table_1_3"];
    unit_trigger_2_2 = [[NSString alloc] initWithFormat:@"unit_trigger_%@",@"table2_2"];
    
    detail_panel_shift_out = [[NSString alloc] initWithFormat:@"detail_panel_shift_out_%@",inSuffix];
    
    detail_view_exit = [[NSString alloc] initWithFormat:@"detail_view_exit_%@",inSuffix];
    detail_view_exit_1_2 = [[NSString alloc] initWithFormat:@"detail_view_exit_%@",@"table1_2"];
    detail_view_exit_2_2 = [[NSString alloc] initWithFormat:@"detail_view_exit_%@",@"table2_2"];
    detail_view_exit_3 = [[NSString alloc] initWithFormat:@"detail_view_exit_%@",@"table3"];
    
    page_refresh = [[NSString alloc] initWithFormat:@"page_refresh_%@",inSuffix];
    share_btn_trigger = [[NSString alloc] initWithFormat:@"share_btn_trigger_%@",inSuffix];
    
    suffix = [[NSString alloc] initWithString:inSuffix];
    
    // Initialization message listener
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationSelector:) name:detail_panel_shift_in object:Nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationSelector:) name:detail_panel_shift_out object:Nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationSelector:) name:unit_trigger object:Nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationSelector:) name:unit_trigger_1_2 object:Nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationSelector:) name:unit_trigger_3_1 object:Nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationSelector:) name:unit_trigger_2_2 object:Nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationSelector:) name:detail_view_exit object:Nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationSelector:) name:detail_view_exit_1_2 object:Nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationSelector:) name:detail_view_exit_2_2 object:Nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationSelector:) name:detail_view_exit_3 object:Nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationSelector:) name:page_refresh object:Nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationSelector:) name:share_btn_trigger object:Nil];
    
    table1_2_link = [[NSString alloc] initWithString:inTable1_2URL];
    table2_2_link = [[NSString alloc] initWithString:inTable2_2URL];
    detail_table_link = [[NSString alloc] initWithString:inTableDetailURL];
    
    titleTopBar = [[TopBar alloc] initWithFrame:CGRectMake(0, 0, PAGE_WIDTH,65)];
    titleTopBar.suffix = inSuffix;
    [titleTopBar initUI:inTopBarTitle logoDisplay:TRUE type:2];
    [self addSubview:titleTopBar];
    
    table1 = [[XMLTable alloc] initWithFrame:CGRectMake(0,TABLE_OFFSET_Y, 
                                                                                            PAGE_WIDTH,LEVEL_1_TABLE_HEIGHT)];
    [table1 configTable:PAGE_WIDTH
                 tableHeight:LEVEL_1_TABLE_HEIGHT
                  tableWidth:TABLE_ROW_HEIGHT
             tableUnitStyle:1];    
    table1.suffix = inSuffix;
    [table1 configMsg];
    [table1 setAtomData:@"/feed/tag"
                   atom:@"tag"
                   feed:@"feed"];
    [table1 triggerByURL:inTable1URL useFeatureImg:FALSE];
    [self addSubview:table1];

    table2 = [[XMLTable alloc] initWithFrame:CGRectMake(0,TABLE_OFFSET_Y, 
                                                        PAGE_WIDTH,LEVEL_1_TABLE_HEIGHT)];
    [table2 configTable:PAGE_WIDTH
            tableHeight:LEVEL_1_TABLE_HEIGHT
             tableWidth:TABLE_ROW_HEIGHT
         tableUnitStyle:1];
    table2.suffix = inSuffix;
    table2.hidden = TRUE;
    [table2 configMsg];
    [table2 setAtomData:@"/feed/tag"
                   atom:@"tag"
                   feed:@"feed"];
    [table2 triggerByURL:inTable2URL useFeatureImg:FALSE];
    [self addSubview:table2];
    
    table3 = [[XMLTable alloc] initWithFrame:CGRectMake(0,TABLE_OFFSET_Y, 
                                                        PAGE_WIDTH,LEVEL_1_TABLE_HEIGHT)];
    [table3 configTable:PAGE_WIDTH
            tableHeight:LEVEL_1_TABLE_HEIGHT
             tableWidth:75
         tableUnitStyle:0];    
    table3.suffix = @"table_1_3";
    table3.hidden = TRUE;
    [table3 configMsg];
    [table3 triggerByURL:inTable3URL useFeatureImg:FALSE];
    [self addSubview:table3];
        
    //init segment
    if (inTagArr != Nil && [inTagArr count]>0)
    {    
        seg = [[UISegmentedControl alloc] initWithItems:inTagArr];
        seg.frame = CGRectMake(35, 80, 250, 40);
        seg.selectedSegmentIndex = 0;
        [seg addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
        [self addSubview:seg];
    }
    
    //the shilft panel-
    table1_2_and_2_2 = [[BaseInfoPanel alloc] initWithFrame:CGRectMake(PAGE_WIDTH,0 , PAGE_WIDTH, PAGE_HEIGHT-30)];
    [table1_2_and_2_2 setUpLogoAndImg:FALSE featuredImg:FALSE];
    [table1_2_and_2_2 setUpReturnBtn:TRUE];
    [table1_2_and_2_2 setInnerSuffix:@"table1_2"];
    [self addSubview:table1_2_and_2_2];
    
    detailPanel  = [[BaseDetailPanel alloc] initWithFrame:CGRectMake(PAGE_WIDTH, 0, PAGE_WIDTH, PAGE_HEIGHT-30)];
    [detailPanel trigger:detail_table_link
            infoBarTitle: @""
                  suffix:@"table3"];
    [self addSubview:detailPanel];

    self.backgroundColor = [ColorUtil stringHexToUIColor:@"0xf0f0f0"];
}

-(void) notificationSelector:(NSNotification *) notification
{
    NSString *curNotificationName = [notification name];
    
    if ([curNotificationName isEqualToString:detail_panel_shift_in] ||
        [curNotificationName isEqualToString:unit_trigger])
    {
        MWFeedItem *tempAtom = [notification object];
        NSString *theLink;
        NSString *theId;
        if (seg.selectedSegmentIndex == 0) 
        {
            theLink = table1_2_link;
            theId = tempAtom.identifier;
        }
        else if (seg.selectedSegmentIndex == 1) 
        {
            theLink = table2_2_link;
            
            //UTF-8 SOLUTION
            NSString * encodedString = (NSString *)CFURLCreateStringByAddingPercentEscapes(
                                                                                           NULL,
                                                                                           (CFStringRef)tempAtom.title,
                                                                                           NULL,
                                                                                           (CFStringRef)@"!*'();:@&=+$,/?%#[]",
                                                                                           kCFStringEncodingUTF8 );
            
            theId = encodedString;
        }

        [table1_2_and_2_2 trigger: [[NSString alloc] initWithFormat:@"%@%@",theLink,theId]
                     infoBarTitle: tempAtom.title
                           suffix:suffix];
        
        [UIView transitionWithView:self  duration:0.4f 
                           options:UIViewAnimationCurveEaseOut  
                        animations:
         ^{
             table1_2_and_2_2.transform = CGAffineTransformMakeTranslation(-PAGE_WIDTH,0);
         }
                        completion:NULL];
    }
    else if ([curNotificationName isEqualToString:detail_panel_shift_out] ||
             [curNotificationName isEqualToString:detail_view_exit])
    {

        [UIView transitionWithView:self duration:0.4f 
                           options:UIViewAnimationCurveEaseOut  
                        animations:
         ^{
             table1_2_and_2_2.transform = CGAffineTransformMakeTranslation(0,0);
             [table1 releaseSelectedNode];
             [table2 releaseSelectedNode];
             [table3 releaseSelectedNode];
         }
                        completion:NULL]; 
    }
    else if ([curNotificationName isEqualToString:unit_trigger_1_2] || 
                [curNotificationName isEqualToString:unit_trigger_3_1])
    {
        MWFeedItem *tempAtom = [notification object];
           
        [detailPanel triggerByBuildId:tempAtom.identifier];
        [detailPanel setUpTitle:tempAtom.title];
                
        [UIView transitionWithView:self  duration:0.4f 
                           options:UIViewAnimationCurveEaseOut  
                        animations:
         ^{
             detailPanel.transform = CGAffineTransformMakeTranslation(-PAGE_WIDTH,0);
         }
         completion:NULL];
    }
    else if([curNotificationName isEqualToString:detail_view_exit_3])
    {
        [UIView transitionWithView:detailPanel duration:0.4f 
                           options:UIViewAnimationCurveEaseOut  
                        animations:
         ^{
             detailPanel.transform = CGAffineTransformMakeTranslation(0,0);
             [table1_2_and_2_2 releaseSelectedNode];
             
         }
                        completion:NULL]; 
    }

}

- (void)segmentAction:(UISegmentedControl*)sender 
{
    table1.hidden = TRUE;
    table2.hidden = TRUE;
    table3.hidden = TRUE;
    
    switch (sender.selectedSegmentIndex) 
    {
        case 0:
            table1.hidden = FALSE;
            break;
        case 1:
            table2.hidden = FALSE;
            break;
        case 2:
            table3.hidden = FALSE;
            break;
        default:
            break;
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

- (void)dealloc
{
    [super dealloc];
}

@end
